//
//  WHYTabBarView.m
//  封装tabBar
//
//  Created by ma c on 15/10/10.
//  Copyright (c) 2015年 吴洪宇. All rights reserved.
//

#import "WHYTabBarView.h"
#import "ViewController.h"
@interface WHYTabBarView ()
{
    UIScrollView *scrollViewTop;
    CGFloat viewWidth;
    CGFloat viewHeight;
    UIView *lineView;
    BOOL lagt;
    NSArray *titleArray;
    NSUInteger arrayCount;
}
@end

@implementation WHYTabBarView




- (instancetype)initWithFrame:(CGRect)frame withWidth:(CGFloat)width withHeight:(CGFloat)height withArray:(NSArray *)arr{
    self=[super initWithFrame:frame];
    if (self) {
        viewWidth=width-15;
        viewHeight=height;
        lagt=YES;
        titleArray=arr;
        arrayCount=titleArray.count;
        [self createTabBarUI];
    }
    return self;
}

- (void)createTabBarUI
{
    //上部分scrollView
    scrollViewTop=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewWidth+15, 40)];
    scrollViewTop.contentSize = CGSizeMake(viewWidth*2, 40);
    scrollViewTop.pagingEnabled=NO;
    scrollViewTop.showsHorizontalScrollIndicator=YES;
    scrollViewTop.bounces=NO;
    scrollViewTop.scrollEnabled=YES;
    [self addSubview:scrollViewTop];
    //下划线
    lineView=[[UIView alloc]initWithFrame:CGRectMake(10, 5, viewWidth/(arrayCount/2)-20,30)];
//    NSLog(@"%ld",arrayCount);
    lineView.backgroundColor=[UIColor lightGrayColor];
    lineView.layer.masksToBounds = YES;
    lineView.layer.cornerRadius = 5;
    [scrollViewTop addSubview:lineView];
    for (int i=0; i<arrayCount; i++) {
        //上部scrollView中的按钮
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake((viewWidth/(arrayCount/2))*i, 0, viewWidth/(arrayCount/2), 40);
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        btn.tag=i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
        [scrollViewTop addSubview:btn];
    }
}

- (void)btnEvent:(UIButton *)btn
{
    lagt=NO;
    lineView.frame=CGRectMake((viewWidth/(arrayCount/2))*btn.tag+10, 5, viewWidth/(arrayCount/2)-20, 30);
        scrollViewTop.contentOffset=CGPointMake((viewWidth/arrayCount)*btn.tag, 0);
    NSString *str=[NSString stringWithFormat:@"%ld",btn.tag];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"123" object:str];
    lagt=YES;
}

- (void) chuanZhi:(UIScrollView *)scrollView
{
    CGFloat tmp=scrollView.contentOffset.x/viewWidth;

    if (tmp==arrayCount-1) {
        scrollViewTop.contentOffset=CGPointMake((viewWidth/arrayCount)*(tmp+1), 0);
    }else{
        scrollViewTop.contentOffset=CGPointMake((viewWidth/arrayCount)*tmp, 0);
    }
    lineView.frame=CGRectMake(((viewWidth-27)/(arrayCount/2))*tmp+5, 5, viewWidth/(arrayCount/2)-10, 30);
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
